/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package processes;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import naming.PathContainer;

/**
 *
 * @author lucmussi
 */
public class InitProperties {

    public static Properties initGeneralProper() throws IOException{
        Properties proper = new Properties();
        try {
            InputStream propsFile = new FileInputStream(System.getProperty("user.dir")+"\\config\\config.properties");
            proper.load(propsFile);
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            try {
                File propFile = new File(System.getProperty("user.dir")+"\\config\\config.properties");
                propFile.getParentFile().mkdirs();
                OutputStream newPropFile = new FileOutputStream(propFile);
                proper.setProperty("defaultDir", "c:\\");
                proper.setProperty("fontColor", "#FFFFFF");
                proper.setProperty("bgColor", "#000000");
                proper.store(newPropFile, "Default Config File");
                newPropFile.close();
                return proper;
            } catch (FileNotFoundException ex1) {
                System.out.println("Todo falló");
            }
        }
        return null;
    }

    public static Properties getEclipsePath() throws IOException{
        Properties proper = new Properties();
        int returnVal;
        try {
            InputStream propsFile = new FileInputStream(System.getProperty("user.dir")+"\\config\\config.properties");
            proper.load(propsFile);
            if(proper.getProperty(PathContainer.ECLIPSEPATH) == null){
                JOptionPane.showMessageDialog(null, "No tiene una ruta de eclipse definida. Seleccione a continuación","Información",JOptionPane.INFORMATION_MESSAGE);
                JFileChooser chooser = new JFileChooser(new File("c:\\"));
                returnVal = chooser.showOpenDialog(null);
                while(returnVal == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith("ipse.exe")){
                    JOptionPane.showMessageDialog(null, "El archivo "+ chooser.getSelectedFile() + " no es un ejecutable de eclipse",
                                                        "Open Error", JOptionPane.ERROR_MESSAGE);
                    returnVal = chooser.showOpenDialog(null);
                }
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    OutputStream newPropFile = new FileOutputStream(System.getProperty("user.dir")+"\\config\\config.properties");
                    proper.setProperty(PathContainer.ECLIPSEPATH, chooser.getSelectedFile().toString());
                    proper.store(newPropFile, "New Config File");
                    newPropFile.close();
                    return proper;
                }
            }
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Archivo de Configuración no encontrado","ERROR", JOptionPane.ERROR_MESSAGE);
            initGeneralProper();
        }
        return null;
    }

    public static Properties getPuttyPath() throws IOException{
        Properties proper = new Properties();
        int returnVal;
        try {
            InputStream propsFile = new FileInputStream(System.getProperty("user.dir")+"\\config\\config.properties");
            proper.load(propsFile);
            if(proper.getProperty(PathContainer.PUTTYPATH) == null){
                JOptionPane.showMessageDialog(null, "No tiene una ruta de PuTTy definida. Seleccione a continuación","Información",JOptionPane.INFORMATION_MESSAGE);
                JFileChooser chooser = new JFileChooser(new File("c:\\"));
                returnVal = chooser.showOpenDialog(null);
                while(returnVal == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith("tty.exe")){
                    JOptionPane.showMessageDialog(null, "El archivo "+ chooser.getSelectedFile() + " no es un ejecutable de putty",
                                                        "Open Error", JOptionPane.ERROR_MESSAGE);
                    returnVal = chooser.showOpenDialog(null);
                }
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    OutputStream newPropFile = new FileOutputStream(System.getProperty("user.dir")+"\\config\\config.properties");
                    proper.setProperty(PathContainer.PUTTYPATH, chooser.getSelectedFile().toString());
                    proper.store(newPropFile, "New Config File");
                    newPropFile.close();
                    return proper;
                }
            }
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Archivo de Configuración no encontrado","ERROR", JOptionPane.ERROR_MESSAGE);
            initGeneralProper();
        }
        return null;
    }

    public static Properties getSqldevPath() throws IOException{
        Properties proper = new Properties();
        int returnVal;
        try {
            InputStream propsFile = new FileInputStream(System.getProperty("user.dir")+"\\config\\config.properties");
            proper.load(propsFile);
            if(proper.getProperty(PathContainer.SQLDEVPATH) == null){
                JOptionPane.showMessageDialog(null, "No tiene una ruta de SQL Developer definida. Seleccione a continuación","Información",JOptionPane.INFORMATION_MESSAGE);
                JFileChooser chooser = new JFileChooser(new File("c:\\"));
                returnVal = chooser.showOpenDialog(null);
                while(returnVal == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith("oper.exe")){
                    JOptionPane.showMessageDialog(null, "El archivo "+ chooser.getSelectedFile() + " no es un ejecutable de SQL Developer",
                                                        "Open Error", JOptionPane.ERROR_MESSAGE);
                    returnVal = chooser.showOpenDialog(null);
                }
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    OutputStream newPropFile = new FileOutputStream(System.getProperty("user.dir")+"\\config\\config.properties");
                    proper.setProperty(PathContainer.SQLDEVPATH, chooser.getSelectedFile().toString());
                    proper.store(newPropFile, "New Config File");
                    newPropFile.close();
                    return proper;
                }
            }
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Archivo de Configuración no encontrado","ERROR", JOptionPane.ERROR_MESSAGE);
            initGeneralProper();
        }
        return null;
    }

    public static Properties getWinscpPath() throws IOException{
        Properties proper = new Properties();
        int returnVal;
        try {
            InputStream propsFile = new FileInputStream(System.getProperty("user.dir")+"\\config\\config.properties");
            proper.load(propsFile);
            if(proper.getProperty(PathContainer.WINSCPPATH) == null){
                JOptionPane.showMessageDialog(null, "No tiene una ruta de WinSCP definida. Seleccione a continuación","Información",JOptionPane.INFORMATION_MESSAGE);
                JFileChooser chooser = new JFileChooser(new File("c:\\"));
                returnVal = chooser.showOpenDialog(null);
                while(returnVal == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith("SCP.exe")){
                    JOptionPane.showMessageDialog(null, "El archivo "+ chooser.getSelectedFile() + " no es un ejecutable de WinSCP",
                                                        "Open Error", JOptionPane.ERROR_MESSAGE);
                    returnVal = chooser.showOpenDialog(null);
                }
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    OutputStream newPropFile = new FileOutputStream(System.getProperty("user.dir")+"\\config\\config.properties");
                    proper.setProperty(PathContainer.WINSCPPATH, chooser.getSelectedFile().toString());
                    proper.store(newPropFile, "New Config File");
                    newPropFile.close();
                    return proper;
                }
            }
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Archivo de Configuración no encontrado","ERROR", JOptionPane.ERROR_MESSAGE);
            initGeneralProper();
        }
        return null;
    }

    @Deprecated
    public static Properties initEclipsePath() throws IOException{
        Properties proper = new Properties();
        InputStream propsFile;
        int returnVal;
        try {
            propsFile = new FileInputStream(System.getProperty("user.dir") + "\\config\\eclipse.properties");
            proper.load(propsFile);
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            try {
                do{
                JOptionPane.showMessageDialog(null, "No tiene una ruta de eclipse definida. Seleccione a continuación","Información",JOptionPane.INFORMATION_MESSAGE);
                OutputStream newPropFile = null;
                JFileChooser chooser = new JFileChooser(new File("c:\\"));
                returnVal = chooser.showOpenDialog(null);
                while(returnVal == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith("ipse.exe")){
                    JOptionPane.showMessageDialog(null, "El archivo "+ chooser.getSelectedFile() + " no es un ejecutable de eclipse",
                                                        "Open Error", JOptionPane.ERROR_MESSAGE);
                    returnVal = chooser.showOpenDialog(null);
                }
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    File propFile = new File(System.getProperty("user.dir")+"\\config\\eclipse.properties");
                    propFile.getParentFile().mkdirs();
                    newPropFile = new FileOutputStream(propFile);
                    proper.setProperty("eclipsePath", chooser.getSelectedFile().toString());
                    proper.store(newPropFile, "Eclipse Path File");
                    newPropFile.close();
                    return proper;
                }
                }while(returnVal == JFileChooser.CANCEL_OPTION);

            } catch (IOException ioex) {
                JOptionPane.showMessageDialog(null, "Error de E/S","ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
        return null;
    }

    @Deprecated
    public static Properties initPuttyPath() throws IOException {
        Properties proper = new Properties();
        InputStream propsFile;
        int returnVal;
        try {
            propsFile = new FileInputStream(System.getProperty("user.dir") + "\\config\\putty.properties");
            proper.load(propsFile);
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            try {
                do{
                JOptionPane.showMessageDialog(null, "No tiene una ruta de PuTTY definida. Seleccione a continuación","Información",JOptionPane.INFORMATION_MESSAGE);
                OutputStream newPropFile = null;
                JFileChooser chooser = new JFileChooser(new File("c:\\"));
                returnVal = chooser.showOpenDialog(null);
                while(returnVal == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith("tty.exe")){
                    JOptionPane.showMessageDialog(null, "El archivo "+ chooser.getSelectedFile() + " no es un ejecutable de PuTTY",
                                                        "Open Error", JOptionPane.ERROR_MESSAGE);
                    returnVal = chooser.showOpenDialog(null);
                }
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    File propFile = new File(System.getProperty("user.dir")+"\\config\\putty.properties");
                    propFile.getParentFile().mkdirs();
                    newPropFile = new FileOutputStream(propFile);
                    proper.setProperty("puttyPath", chooser.getSelectedFile().toString());
                    proper.store(newPropFile, "PuTTY Path File");
                    newPropFile.close();
                    return proper;
                }
                }while(returnVal == JFileChooser.CANCEL_OPTION);
            } catch (IOException ioex) {
                JOptionPane.showMessageDialog(null, "Error de E/S","ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
        return null;
    }

    @Deprecated
    public static Properties initSqldevPath() throws IOException {
        Properties proper = new Properties();
        InputStream propsFile;
        int returnVal;
        try {
            propsFile = new FileInputStream(System.getProperty("user.dir") + "\\config\\sqldev.properties");
            proper.load(propsFile);
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            try {
                do{
                JOptionPane.showMessageDialog(null, "No tiene una ruta de SQL Developer definida. Seleccione a continuación","Información",JOptionPane.INFORMATION_MESSAGE);
                OutputStream newPropFile = null;
                JFileChooser chooser = new JFileChooser(new File("c:\\"));
                returnVal = chooser.showOpenDialog(null);
                while(returnVal == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith("oper.exe")){
                    JOptionPane.showMessageDialog(null, "El archivo "+ chooser.getSelectedFile() + " no es un ejecutable de SQL Developer",
                                                        "Open Error", JOptionPane.ERROR_MESSAGE);
                    returnVal = chooser.showOpenDialog(null);
                }
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    File propFile = new File(System.getProperty("user.dir")+"\\config\\sqldev.properties");
                    propFile.getParentFile().mkdirs();
                    newPropFile = new FileOutputStream(propFile);
                    proper.setProperty("sqldevPath", chooser.getSelectedFile().toString());
                    proper.store(newPropFile, "SQL Developer Path File");
                    newPropFile.close();
                    return proper;
                }
                }while(returnVal == JFileChooser.CANCEL_OPTION);
            } catch (IOException ioex) {
                JOptionPane.showMessageDialog(null, "Error de E/S","ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
        return null;
    }

    @Deprecated
    public static Properties initWinscpPath() throws IOException {
        Properties proper = new Properties();
        InputStream propsFile;
        int returnVal;
        try {
            propsFile = new FileInputStream(System.getProperty("user.dir") + "\\config\\winscp.properties");
            proper.load(propsFile);
            propsFile.close();
            return proper;
        } catch (FileNotFoundException ex) {
            try {
                do{
                JOptionPane.showMessageDialog(null, "No tiene una ruta de WinSCP definida. Seleccione a continuación","Información",JOptionPane.INFORMATION_MESSAGE);
                OutputStream newPropFile = null;
                JFileChooser chooser = new JFileChooser(new File("c:\\"));
                returnVal = chooser.showOpenDialog(null);
                while(returnVal == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith("SCP.exe")){
                    JOptionPane.showMessageDialog(null, "El archivo "+ chooser.getSelectedFile() + " no es un ejecutable de WinSCP",
                                                        "Open Error", JOptionPane.ERROR_MESSAGE);
                    returnVal = chooser.showOpenDialog(null);
                }
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    File propFile = new File(System.getProperty("user.dir")+"\\config\\winscp.properties");
                    propFile.getParentFile().mkdirs();
                    newPropFile = new FileOutputStream(propFile);
                    proper.setProperty("winscpPath", chooser.getSelectedFile().toString());
                    proper.store(newPropFile, "WinSCP Path File");
                    newPropFile.close();
                    return proper;
                }
                }while(returnVal == JFileChooser.CANCEL_OPTION);
            } catch (IOException ioex) {
                JOptionPane.showMessageDialog(null, "Error de E/S","ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
        return null;
    }

}
